package com.example.campusforumapi.controller;

import com.example.campusforumapi.entity.Comment;
import com.example.campusforumapi.entity.User;
import com.example.campusforumapi.service.CommentService;
import com.example.campusforumapi.service.UserService;
import com.example.campusforumapi.util.PageResult;
import com.example.campusforumapi.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/comments")
public class CommentController extends BaseController{
    @Autowired
    private CommentService commentService;

    /*
     * 按条件  分页查询
     * */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize
            , Integer userId, String commentType, Integer commentId, Integer targetId,String content){

        //        查询并封装分页结果集
        PageResult commentPage = commentService.getCommentPage(page,pageSize,userId,commentType,commentId,targetId,content);

        return Result.success(commentPage);
    }
    //    删除一条记录
    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        commentService.delComment(ids);
        return Result.success();
    }
    //    修改一条记录
    @PutMapping("/update")
    public Result update(@RequestBody Comment comment){
        if(commentService.updComment(comment) > 0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败，该评论不存在");
        }

    }
}
